import ScrollBarLayout from '@/components/ScrollBarLayout';
import Cls from 'classnames';
import { useContext, useEffect, useState } from 'react';
import { ChangeControllerContext } from '../common/useOAContext';
import { DatePicker, Form, Input, Select, Spin, Tooltip, TreeSelect, message, Button } from 'antd';
import { useDescriptions, Descriptions } from '@/components/Descriptions';
import { downloadTemplate, getFileNameNotType, getLoadMapList, getTenantID, splitFileUrl } from '@/utils';
import MyIcon from '@/components/MyIcon';
import { formatDecimal } from '../../Acommon';
import UpdateAssetList from './UpdateContractList';
import { TypographyTitle } from '@/components/Typography/TypographyTitle';
import moment from 'moment';
import { getBaseUrl } from '@/services/urlconst';
import { readReadShowFileService } from '@/services/BaseLayout';
import { EyeOutlined } from '@ant-design/icons';

export const DetailInfoCenterViewMainOA: React.FC = () => {
  /** 引入上下文 */
  const { styleFixCls, infoObj } = useContext(ChangeControllerContext);

  const [changeFromRef] = Form.useForm(); // 表单实例

  /** 轻阅读预览 */
  const { readReadShowFileRun } = readReadShowFileService();

  /**轻阅读预览 */
  const onOpenReadOfdWord = (fieldUrl: string) => {
    readReadShowFileRun({ fieldPath: `${getBaseUrl()}${fieldUrl}` });
  };

  useEffect(() => {
    if (infoObj?.id) {
      BasicMethods?.setFieldsValue({
        ...infoObj,
        createTime: moment(infoObj?.createTime).format('YYYY-MM-DD'),
        expectSum: formatDecimal(infoObj?.expectSum),
      });
      ReMethods?.setFieldsValue({
        ...infoObj,
        createTime: moment(infoObj?.createTime).format('YYYY-MM-DD'),
        expectSum: formatDecimal(infoObj?.expectSum),
      });
    }
  }, [infoObj]);

  // 表格表单配置信息
  const [config, BasicMethods] = useDescriptions({
    labelWidth: 200,
    basicForm: changeFromRef,
    schemas: [
      {
        field: `createTime`,
        label: `申请日期`,
        component: 'Input',
        disabled: true,
        placeholder: '自动带出',
      },
      {
        field: `applyCode`,
        label: `流水单号`,
        component: 'Input',
        disabled: true,
        placeholder: '自动带出',
      },
      {
        field: `createrName`,
        label: `申请人`,
        component: 'Input',
        placeholder: '自动带出',
        disabled: true,
      },
      {
        field: `createPhone`,
        label: `联系电话`,
        component: 'Input',
        placeholder: '自动带出',
        disabled: true,
      },
      {
        field: `createDepartmentName`,
        label: `申请部门/单位`,
        component: 'Input',
        placeholder: '自动带出',
        disabled: true,
      },
      {
        field: `createPositionName`,
        label: `职务岗位`,
        component: 'Input',
        placeholder: '自动带出',
        disabled: true,
      },
    ],
  });

  // 表格表单配置信息
  const [ReConfig, ReMethods] = useDescriptions({
    labelWidth: 200,
    basicForm: changeFromRef,
    schemas: [
      {
        field: `applyTitle`,
        label: `事项名称`,
        component: 'Input',
        disabled: true,
        colSpan: 0,
        placeholder: '自动带出',
      },
      {
        field: `content`,
        label: `维保内容`,
        component: 'Render',
        disabled: true,
        colSpan: 2,
        allowClear: true,
        showCount: true,
        render: (text) => {
          if (text) {
            return <Input.TextArea disabled value={text} showCount maxLength={200} autoSize={{ minRows: 2, maxRows: 5 }} />;
          } else {
            return (
              <div
                style={{
                  height: '100%',
                  display: 'flex',
                  alignItems: 'center',
                  marginLeft: '8px',
                }}
              >
                --
              </div>
            );
          }
        },
      },
      {
        field: `applyFile`,
        label: `相关附件`,
        component: 'Render',
        required: false,
        placeholder: '请上传',
        render: (text, record) => {
          const fileName = 'applyFile';
          return (
            <>
              <div
                style={{
                  height: '100%',
                  display: 'flex',
                  alignItems: 'center',
                  marginLeft: '8px',
                }}
              >
                {record?.[fileName]
                  ? splitFileUrl(record?.[fileName])?.map((e, i) => {
                      return (
                        <div>
                          <Tooltip title={e.attachName} key={new Date().getTime() * 25615265152 + e?.attachName}>
                            <a
                              onClick={() => {
                                message.success('下载中，请稍后...');
                                downloadTemplate('/assetInfo/downloadFile', e?.attachName, e?.attachUrl, () => {});
                              }}
                            >
                              <MyIcon type="icon-file-text-fill" />
                              &nbsp;
                              {getFileNameNotType(e.attachName, 8)}
                            </a>
                          </Tooltip>
                          {/* 轻阅读预览 */}
                          <Tooltip title={'在线预览'}>
                            <Button type="link" size="small" onClick={() => onOpenReadOfdWord(e?.attachUrl)} icon={<EyeOutlined />} />
                          </Tooltip>
                        </div>
                      );
                    })
                  : '--'}
              </div>
            </>
          );
        },
      },
    ],
  });

  return (
    <>
      <div className="commonTabsBodyDom">
        <div className={Cls('tabsBodyInfoDom', `${styleFixCls}updateFormBox`)}>
          <ScrollBarLayout style={{ padding: '20px 10%' }}>
            <Spin spinning={false}>
              <TypographyTitle children={'资产维保申请'} style={{ textAlign: 'center', borderLeft: 'none' }} level={1} />
              <TypographyTitle children={'基本信息'} style={{ margin: '25px 0px 20px' }} level={2} />
              <Descriptions config={config} />
              <TypographyTitle children={'资产维保信息'} style={{ margin: '25px 0px 20px' }} level={2} />
              <Descriptions config={ReConfig} />
              <TypographyTitle children={'资产详单'} style={{ margin: '25px 0px 20px' }} level={2} />
              <UpdateAssetList packList={infoObj.massetMaintenDetailList ? infoObj.massetMaintenDetailList : []} />
            </Spin>
          </ScrollBarLayout>
        </div>
      </div>
    </>
  );
};
